package com.admin.education.controllers.api.v1;

import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.sponsor.Request.CreateSponsorRequest;
import com.admin.education.dataTransferObject.sponsor.SponsorTransfer;
import com.admin.education.services.Interfaces.ISponsorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@RestController
@RequestMapping("/v1/sponsors")
public class SponsorController {
    @Autowired
    private ISponsorService sponsorService;
    @GetMapping("")
    public Callable<IResponseMessage> querySponsors(@Valid PageRequest pageRequest){
        return  new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {

                IResponseMessage responseMessage = null;

                ServicesExcuteResult<PageResponse<SponsorTransfer>> result = sponsorService.querySponsors(pageRequest);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<SponsorTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PostMapping("")
    public  Callable<IResponseMessage> createSponsor(@RequestBody @Valid CreateSponsorRequest createSponsorRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = sponsorService.createSponsor(createSponsorRequest);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
